package com.zpkj.portal.utils.des3;

import java.security.Key;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;

public class Des3Token {
	// 密钥 长度不得小于24
    private final static String secretKey = "1379246805559736512375312468" ;
    // 向量 可有可无 token解密需要后台保留，前端无需知晓
    private final static String iv = "36512469";
    // 加解密统一使用的编码方式
    private final static String encoding = "utf-8";

    /**
    * 3DES加密
    * @param plainText 普通文本
    * @throws Exception
    */
    public static String encode(String plainText) throws Exception {
         Key deskey = null;
         DESedeKeySpec spec = new DESedeKeySpec(secretKey .getBytes());
         SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "desede");
         deskey = keyfactory.generateSecret(spec);

         Cipher cipher = Cipher.getInstance( "desede/CBC/PKCS5Padding");
         IvParameterSpec ips = new IvParameterSpec( iv.getBytes());
         cipher.init(Cipher. ENCRYPT_MODE, deskey, ips);
         byte[] encryptData = cipher.doFinal( plainText.getBytes( encoding));
         return Base64.encode( encryptData);
   }

    /**
    * 3DES解密
    *
    * @param encryptText
    *            加密文本
    * @return
    * @throws Exception
    */
    public static String decode(String encryptText) throws Exception {
         Key deskey = null;
         DESedeKeySpec spec = new DESedeKeySpec( secretKey.getBytes());
         SecretKeyFactory keyfactory = SecretKeyFactory.getInstance( "desede");
         deskey = keyfactory. generateSecret( spec);
         Cipher cipher = Cipher.getInstance( "desede/CBC/PKCS5Padding" );
         IvParameterSpec ips = new IvParameterSpec( iv.getBytes());
         cipher. init(Cipher. DECRYPT_MODE, deskey, ips);

         byte[] decryptData = cipher. doFinal(Base64.decode(encryptText ));

         return new String( decryptData, encoding);
   }
   
//    public static void main(String args[]) throws Exception{
//       System. out.println( "----加密后-----：" + Des3Utils. encode( "123") );
//       System.out.println( "----解密后-----：" + Des3Utils.decode( "dHUtOJXUpmo="));
//   }
}
